package com.jee.ssm.modules.posuserBerth.controller;

import com.jee.ssm.model.PosUser;
import com.jee.ssm.model.PosuserBerth;
import com.jee.ssm.modules.posUser.services.PosUserService;
import com.jee.ssm.modules.posuserBerth.services.PosuserBerthService;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.common.utils.UUIDFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 收费员绑定车位管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/posuserBerth")
public class PosuserBerthController extends AdminBaseController<PosuserBerth> {


    /**
     * 进入收费员绑定车位添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("posuserBerth:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/posuserBerth/add";
    }


    /**
     * 进入收费员绑定车位编辑页面
     * @param model 返回收费员绑定车位的容器
     * @param id 收费员绑定车位id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("posuserBerth:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",posuserBerthService.selectById(id));
        return "manager/posuserBerth/edit";
    }


    /**
     * 收费员绑定车位添加
     * @param posuserBerth 带id的收费员绑定车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("posuserBerth:add")
    @AdminControllerLog(description = "添加收费员绑定车位" )
    public Tip save(PosuserBerth posuserBerth)  {

        try {
            posuserBerthService.insert(posuserBerth);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改收费员绑定车位
     * @param posuserBerth 带id的收费员绑定车位对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("posuserBerth:edit")
    @AdminControllerLog(description = "修改收费员绑定车位" )
    public Tip update(PosuserBerth posuserBerth) {

        try {
            posuserBerthService.updateById(posuserBerth);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除收费员绑定车位
     * @param id 收费员绑定车位id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("posuserBerth:delete")
    @AdminControllerLog(description = "删除收费员绑定车位" )
    public Tip delete(@RequestParam String id) {

        try {
            posuserBerthService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 收费员绑定车位id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("posuserBerth:delete")
    @AdminControllerLog(description = "批量删除收费员绑定车位" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            posuserBerthService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找收费员绑定车位
     * @param id 收费员绑定车位id
     * @return 收费员绑定车位对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("posuserBerth:list")
    public PosuserBerth find(@RequestParam String id) {

        return posuserBerthService.selectById(id);
    }


    /**
     * 获取收费员绑定车位列表 获取全部 不分页
     * @param request 请求参数
     * @return 收费员绑定车位列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("posuserBerth:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取收费员绑定车位列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 收费员绑定车位列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("posuserBerth:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {
        modelMap.put("pageInfo",posuserBerthService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/posuserBerth/list";
    }

    /**
     * 更改用户绑定的车位信息
     * @return
     */
    @RequestMapping("/updateUserBerth")
    @ResponseBody
    public   Tip updateUserBerth(@RequestParam("berthIds") List<String> berthIds,String id){

        try {
            PosUser posUser = posUserService.selectById(id);
            if(posUser !=null){
                posuserBerthService.deleteByUserId(id);
                for(String berthId:berthIds){ //车位的ID
                    if(berthId != null && !"".equals(berthId)){
                        PosuserBerth posuserBerth = new PosuserBerth(UUIDFactory.getStringId(),id,posUser.getParkId(),posUser.getParkName(),berthId);
                        posuserBerthService.insert(posuserBerth);
                    }
                }
                return  new Tip("设置成功");
            }else{
                return  new Tip(2,"未获取到收费员的信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  new Tip(2,"设置失败");
        }

    }


    //---------------------------- property -------------------------------

    @Resource
    private PosuserBerthService posuserBerthService;

    @Resource
    private PosUserService posUserService;

}
